package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.drug.entity.bo.DrugStockBo;
import com.zp.drug.entity.vo.AutoPrescriptionVo;
import com.zp.drug.entity.vo.DrugStockVo;
import com.zp.drug.service.IDrugStockService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 药品库存控制器
 * 前端访问路由地址为:/drug/stock
 *
 * @author zhang peng
 * @ date 2023-06-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock")
public class DrugStockController extends BaseController {

    private final IDrugStockService iDrugStockService;

    /**
     * 查询药品库存列表 (分页)
     */
    @SaCheckPermission("base:drugStock:list")
    @GetMapping("/selectPage")
    public R<Map<String, Object>> selectPage (DrugStockBo bo, PageQuery pageQuery) {
        return R.ok(iDrugStockService.queryPageList(bo, pageQuery));
    }

    /**
     * 查询药品库存列表 (不分页)
     */
    @SaCheckPermission("base:drugStock:list")
    @GetMapping("/list")
    public R<List<DrugStockVo>> list (DrugStockBo bo) {
        List<DrugStockVo> list = iDrugStockService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品库存列表
     */
    @SaCheckPermission("base:drugStock:export")
    @Log(title = "药品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (DrugStockBo bo, HttpServletResponse response) {
        List<DrugStockVo> list = iDrugStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品库存", DrugStockVo.class, response);
    }

    /**
     * 获取药品库存详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:drugStock:list")
    @GetMapping("/{id}")
    public R<DrugStockVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugStockService.queryById(id));
    }

    /**
     * 新增药品库存
     */
    @SaCheckPermission("base:drugStock:add")
    @Log(title = "药品库存", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody DrugStockBo bo) {
        return toAjax(iDrugStockService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品库存
     */
    @SaCheckPermission("base:drugStock:edit")
    @Log(title = "药品库存", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody DrugStockBo bo) {
        return toAjax(iDrugStockService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品库存
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:drugStock:remove")
    @Log(title = "药品库存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugStockService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 处方自动补全
     *
     * @param vo
     * @return
     */
    @PostMapping("/autoPrescriptionList")
    public R<List<AutoPrescriptionVo>> autoPrescriptionList (@RequestBody AutoPrescriptionVo vo) {
        return R.ok(iDrugStockService.autoPrescriptionList(vo));
    }

    /**
     * 药品出库自动补全
     *
     * @param vo
     * @return
     */
    @PostMapping("/autoExportList")
    public R<List<AutoPrescriptionVo>> autoExportList(@RequestBody AutoPrescriptionVo vo) {
        return R.ok(iDrugStockService.autoExportList(vo));
    }

//    /**
//     * 获取盘点药品明细（通过批号等字段分组）
//     *
//     * @param bo
//     * @return
//     */
//    @PostMapping("/getInventoryCheckList")
//    public R<List<DrugStockVo>> getInventoryCheckList(@RequestBody DrugStockBo bo) {
//        return R.ok(iDrugStockService.getInventoryCheckList(bo));
//    }
}
